import { ref } from 'vue'
import PagePopup from '@/components/page-popup'

type CallbackFn = (item?: any) => void

export const usePagePopup = (
  newHandleCallback?: CallbackFn,
  editHandleCallback?: CallbackFn
) => {
  const popupInfo = ref({})
  const pagePopupRef = ref<InstanceType<typeof PagePopup>>()
  const handleNewData = () => {
    popupInfo.value = {}
    if (pagePopupRef.value) {
      pagePopupRef.value.dialogVisible = true
    }
    newHandleCallback && newHandleCallback()
  }

  const handleEditData = (item: any) => {
    //回显赋值
    popupInfo.value = { ...item }
    if (pagePopupRef.value) {
      pagePopupRef.value.dialogVisible = true
    }
    editHandleCallback && editHandleCallback(item)
  }

  return [popupInfo, pagePopupRef, handleNewData, handleEditData]
}
